# include <bits/stdc++.h>
# define MAXN 50050
using namespace std;

inline int gn() {
    int k = 0, f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for (; isdigit(c); c = getchar()) k = k * 10 + c - '0';
    return k * f;
}

vector<int> to[MAXN], ans[MAXN];
int n, m, ans_cnt, dfn[MAXN], low[MAXN];

void dfs(int u) {
    static int cnt = 0;
    static int stack[MAXN], top;
    static bool ins[MAXN];
    dfn[u] = low[u] = ++cnt;
    stack[top++] = u;
    ins[u] = 1;
    int siz = (int) to[u].size();
    for(int i = 0, v; i < siz; i++) {
        v = to[u][i];
        if (!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if (ins[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u]) {
        register vector<int> &p = ans[ans_cnt];
        do {
            p.push_back(stack[--top]);
            ins[stack[top]] = 0;
        } while (stack[top] ^ u);
        sort(p.begin(), p.end());
        ++ans_cnt;
    }
}

inline bool cmp(const vector<int> &a, const vector<int> &b) {
    if (a.size() ^ b.size()) return a.size() > b.size();
    for (unsigned int i = 0; i < a.size(); ++i)
        if (a[i] ^ b[i]) return a[i] < b[i];
    return false;
}

int main() {
    freopen("classroom.in", "r", stdin);
    freopen("classroom.out", "w", stdout);
    n = gn(), m = gn();
    for (int i = 1; i <= m; i++) {
        int f = gn(), t = gn();
        to[f].push_back(t);
        if (gn() == 2) to[t].push_back(f);
    }
    for (int i = 1; i <= n; i++) dfs(i);
    sort(ans, ans + ans_cnt, cmp);
    printf("%d\n", ans[0].size());
    for (int i = 0; i < ans[0].size(); i++) {
        printf("%d ", ans[0][i]);
    }
}